function sampleCars(source_path,i_itr,dest_path, n_sample)
%test code
%{
source_path = 'D:/Projects/AoT/results_n_partition_3';
dest_path ='D:/Projects/AoT/samples';
sampleCars(source_path,5,dest_path, 2)
%}

for i = 1:n_sample
    dest_sub_path = [dest_path '/sample' num2str(i)];
    if( ~exist(dest_sub_path,'dir'))
        mkdir(dest_sub_path);
    else
        fprintf('warning, target path already exist!!\n, %s',dest_sub_path);
    end
    sampleOneCar(source_path,i_itr,dest_sub_path);
end
end



function sampleOneCar(source_path,i_itr,dest_path)
path_tree = cell(0);
path_tree{1}=source_path;


% create configuration file;
conf_file_name =[dest_path '/template.config'];
p_conf_file=fopen(conf_file_name,'w');


tree_list = dir([source_path '/tree*']);
for i_tree = 1:length(tree_list)
    current_path =[path_tree{end} '/' tree_list(i_tree).name '/itrEM' num2str(i_itr)];
    if ~isdir(current_path)
        %    D:\Projects\AoT\results_n_partition_3\tree_01\itrEM1
        continue;
    end
    
    % create tree folder in target path
    target_tree_path = [dest_path '/' tree_list(i_tree).name];
    if ~exist(target_tree_path,'dir')
        mkdir(target_tree_path);
    end
    
    path_tree{end+1}=current_path;
    cluster_list =dir( [current_path '/cluster*']);
    n_cluster= length(cluster_list);
    i_cluster = randi([1 n_cluster],1);
    
    current_path=[path_tree{end} '/cluster_' num2str(i_cluster)];
    path_tree{end+1}=current_path;
    
    tan_list = dir([current_path '/tan*']);
    for i_tan = 1:length(tan_list)
        current_path = [ path_tree{end} '/' tan_list(i_tan).name];
        path_tree{end+1}=current_path;
        
        p_shape_list =dir([current_path '/*.pshape']);
        if isempty(p_shape_list)
            fprintf('warning, no pshape file in directory: %s',current_path);
        end
        file_name = p_shape_list(1).name;
        tmp_data = sscanf(file_name,'image_%d_shape_%d_deform%d.pshape');
        i_shape = tmp_data(2);
        shape_file_name = ['canonical_' num2str(i_shape) '.shape'];
        if ~exist([current_path '/' shape_file_name ],'file');
            fprintf('warning, no shape file %s',[current_path '/' shape_file_name ]);
        end
        % create target directory, copy shape file, plane file, and
        % append configuration file
        target_tan_path = [target_tree_path '/' tan_list(i_tan).name];
        mkdir(target_tan_path);
        copyfile([current_path '/' shape_file_name],...
            [target_tan_path '/' shape_file_name]);
        copyfile([current_path '/plane_geometry.txt'],...
            [target_tan_path '/plane_geometry.txt']);
        fprintf(p_conf_file,'%s\n',[target_tan_path]);
        fprintf(p_conf_file,'%d\n',i_shape+1);
        % render shape and save the rendered image
        img = drawSVGtoImage([current_path '/' shape_file_name]);img = 255 -img;
        %img= SampleDeformedShape([current_path '/' shape_file_name],18,7);
        alpha = double(img<200);
        %[img alpha]=drawColorModel([current_path '/color_model.color'],img);
        imwrite(img,[target_tan_path '/' shape_file_name '.png' ],'Alpha',alpha);
        
        path_tree=path_tree(1:end-1);
    end
    path_tree=path_tree(1:end-2);
end
fclose(p_conf_file);
end
